//
//  CYNFlutterViewController.m
//  App
//
//  Created by blank on 2019-01-23.
//    Copyright © 2019 NowStudio Co.,Ltd. All rights reserved.
//

#import "CYNFlutterViewController.h"
#import <Flutter/Flutter.h>
#import <Masonry/Masonry.h>

#import "AppDelegate.h"

@interface CYNFlutterViewController ()

@property(nonatomic, retain) FlutterViewController *flutterViewController;

@property(nonatomic, retain) FlutterMethodChannel *flutterMethodChannel;

@end

@implementation CYNFlutterViewController

- (void)didInitialize {
    [super didInitialize];
    // init 时做的事情请写在这里
}

- (void)initSubviews {
    [super initSubviews];
    // 对 subviews 的初始化写在这里
    [self addChildViewController:self.flutterViewController];
    [self.view addSubview:self.flutterViewController.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 对 self.view 的操作写在这里
    [self.flutterMethodChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
        NSDictionary *value = call.arguments;
        BOOL state = [value[@"code"] boolValue];
        NSLog(@"state ======= %d", state);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"com.yutong.app.flutter.scroll" object:@(state)];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self.flutterViewController.view mas_remakeConstraints:^(MASConstraintMaker *make) {
         make.edges.mas_equalTo(self.view);
    }];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = @"<##>";
}

- (FlutterViewController *)flutterViewController {
    if (!_flutterViewController) {
        FlutterEngine *flutterEngine = [AppDelegate sharedAppDelegate].flutterEngine;
        _flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
    }
    return _flutterViewController;
}

- (FlutterMethodChannel *)flutterMethodChannel {
    if (!_flutterMethodChannel) {
        _flutterMethodChannel = [FlutterMethodChannel methodChannelWithName:@"com.yutong.app.flutter.method"
                binaryMessenger:self.flutterViewController];
    }
    return _flutterMethodChannel;
}


@end
